package com.rc.saas.tenant.controller.enterprise;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.component.UploadComponent;
import com.rc.saas.tenant.model.enterprise.RcEnterpriseLevelSetting;
import com.rc.saas.tenant.model.tenant.TenantRole;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.tenant.TenantRoleSearch;
import com.rc.saas.tenant.service.enterprise.RcEnterpriseLevelSettingService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

@Controller
@RequestMapping("/enterprise/level/setting")
public class RcEnterpriseLevelSettingController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RcEnterpriseLevelSettingService enterpriseLevelSettingService;

    @Autowired
    private UploadComponent uploadComponent;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String enterpriseMemberPage() {
        return "/templates/enterprise/level/enterpriseLevel";
    }

    @RequestMapping(value = "/addEnterpriseLevel", method = RequestMethod.GET)
    public String addEnterPriseLevel() {
        return "/templates/enterprise/level/addEnterpriseLevel";
    }

    @RequestMapping(value = "/editEnterpriseLevel/{id}", method = RequestMethod.GET)
    public String addEnterPriseLevel(Model model, @PathVariable("id")String id) {
        RcEnterpriseLevelSetting enterpriseLevelSetting = enterpriseLevelSettingService.selectByPrimaryKey(id);
        model.addAttribute("enterpriseLevelSetting", enterpriseLevelSetting);
        return "/templates/enterprise/level/editEnterpriseLevel";
    }

    @ResponseBody
    @RequestMapping("/listPageLevelSetting")
    public WebPageInfo listPageLevelSetting(String levelName, @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        PageInfo<RcEnterpriseLevelSetting> pageInfo = enterpriseLevelSettingService.listPageLevelSetting(levelName, pageNo, pageSize);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 导出企业等级列表
     */
    @ResponseBody
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public TResponse export(String levelName, @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                            @RequestParam(value = "pageSize",defaultValue = "1000")Integer pageSize, HttpServletResponse response) {
        try {
            response.setHeader("Set-Cookie", "fileDownload=true; path=/");
            PageInfo<RcEnterpriseLevelSetting> pageInfo = enterpriseLevelSettingService.listPageLevelSetting(levelName, pageNo, pageSize);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("企业等级列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), RcEnterpriseLevelSetting.class).sheet("企业等级列表").doWrite(pageInfo.getList());
            return null;
        } catch (Exception e) {
            logger.error("导出企业等级列表失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping("/updateLevelSetting")
    public TResponse updateLevelSetting(RcEnterpriseLevelSetting enterpriseLevelSetting,
                                        @RequestParam(value = "file1", required = false) MultipartFile file1) {
        TenantUser user = ShiroUtils.getSessionUser();
        if (file1 != null && file1.getSize() != 0 && file1.getContentType().contains("image")) {
            String imgUrl = uploadComponent.uploadFile(user.getTenantCode(), file1);
            enterpriseLevelSetting.setLevelLogo(imgUrl);
        }
        enterpriseLevelSettingService.updateLevelSetting(enterpriseLevelSetting);
        return TResponse.SUCCESS();
    }

    @ResponseBody
    @RequestMapping("/add")
    public TResponse addLevelSetting(RcEnterpriseLevelSetting enterpriseLevelSetting,
                                     @RequestParam(value = "file1", required = false) MultipartFile file1) {
        TenantUser user = ShiroUtils.getSessionUser();
        if (file1 != null && file1.getSize() != 0 && file1.getContentType().contains("image")) {
            String imgUrl = uploadComponent.uploadFile(user.getTenantCode(), file1);
            enterpriseLevelSetting.setLevelLogo(imgUrl);
        }
        enterpriseLevelSetting.setEnterpriseLevel(enterpriseLevelSetting.getLevelName());
        enterpriseLevelSettingService.addLevelSetting(enterpriseLevelSetting);
        return TResponse.SUCCESS();
    }

    @ResponseBody
    @RequestMapping("/deleteLevelSetting")
    public TResponse deleteLevelSetting(String enterpriseLevel) {
        enterpriseLevelSettingService.deleteByPrimaryKey(enterpriseLevel);
        return TResponse.SUCCESS();
    }


}
